package org.tgcloud.group.core.WXServiceUtils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.tgcloud.group.core.HttpClientUtil;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.core.WXUtils.WeiConfig;

import java.util.Map;

/**
 * @author: zhy
 * @create: 2023-12-31 09:18
 * @description:
 */
@Service
public class WxService {

    @Autowired
    private HttpClientUtil clientUtil;

    private final static Logger logger = LoggerFactory.getLogger(WxService.class);

    /** 获取ticket地址 */
    private final static String GET_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";

    private final static String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    /** appid */
    public final static String APP_ID = "wxe9b9b2010b7529b4";

    /** appsecret */
    public final static String APP_SECRET = "25dc5efc779dfc99d29bfd560899d72e";

    private static Ticket TICKET;

    private static AccessToken AccessToken;


    public static void getAccessTokenLong() {
//        if (StringUtil.isNotBlank(WxService.serverAccessToken)){
//            return WxService.serverAccessToken;
//        }
//        String appId = WxService.APP_ID;
//        String appSecret = WxService.APP_SECRET;
//        String msg = clientUtil.doGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret, null);
//        Map info = JSON.parseObject(msg, new TypeReference<Map>() {
//        });
//        logger.error("获取token:" + msg);
//        WxService.serverAccessToken = info.get("access_token").toString();
//        return info.get("access_token").toString();


        // RestTemplate
        RestTemplate restTemplate = new RestTemplate();
        String url = GET_ACCESS_TOKEN_URL.replace("APPID", APP_ID).replace("APPSECRET", APP_SECRET);
        String forObject = restTemplate.getForObject(url, String.class);
        System.out.println("=========>"+forObject);
        JSONObject jsonObject = JSONUtil.parseObj(forObject);
        String accessToken = jsonObject.getStr("access_token");
        String expiresIn = jsonObject.getStr("expires_in");
        // 创建token对象,并存起来
        AccessToken = new AccessToken(accessToken,expiresIn);
    }

    /**
     * 向外暴露的获取AccessToken方法
     * @return
     */
    public static String getAccessToken(){
        if (AccessToken == null || AccessToken.isExpired()){
            getAccessTokenLong();
        }
        return AccessToken.getAccessToken();
    }

    /**
     * 获取ticket
     */
    private static void getTicket(){
// RestTemplate
        RestTemplate restTemplate = new RestTemplate();
        String url = GET_TICKET_URL.replace("ACCESS_TOKEN",getAccessToken());
        String forObject = restTemplate.getForObject(url, String.class);
        JSONObject jsonObject = JSONUtil.parseObj(forObject);
        String ticket = jsonObject.getStr("ticket");
        String expiresIn = jsonObject.getStr("expires_in");
// 创建token对象,并存起来
        TICKET = new Ticket(ticket,expiresIn);
    }

    /**
     * 向外暴露的获取Ticket方法
     * @return
     */
    public static String getJsSdkTicket(){
        if (TICKET == null || TICKET.isExpired()){
            getTicket();
        }
        return TICKET.getTicket();
    }


}
